package com.cmw.auntapp.ui;

import android.os.Bundle;
import android.util.Log;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cmw.auntapp.R;
import com.cmw.auntapp.adapter.OrderListAdapter;
import com.cmw.auntapp.constant.CommandType;
import com.cmw.auntapp.core.base.vo.DataResult;
import com.cmw.auntapp.core.util.DateUtil;
import com.cmw.auntapp.core.util.FastJsonUtil;
import com.cmw.auntapp.listener.BackListener;
import com.cmw.auntapp.model.OrderModel;
import com.cmw.auntapp.presenter.OrderPresenter;
import com.fantasy.doubledatepicker.DoubleDateSelectDialog;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 用户中心--> 订单列表
 * @Auth chengmingwei
 * @date 2021-04-19 20:39
 */
public class OrderListActivity extends BaseActivity {

    @BindView(R.id.tv_date_range)
    TextView tvDateRange;

    @BindView(R.id.lv_order_list)
    ListView lvOrderList;

    @BindView(R.id.tv_receivableAmount)
    TextView tvReceivableAmount;

    @BindView(R.id.tv_receivedAmount)
    TextView tvReceivedAmount;

    private String startDate = null;
    private String endDate = null;

    private List<OrderModel> orderList = new ArrayList<>();

    private OrderListAdapter orderListAdapter;

    private DoubleDateSelectDialog dateSelectDialog;

    private OrderPresenter orderPresenter;

    @Override
    protected int getActivityLayout() {
        return R.layout.activity_order_list;
    }

    public void setOrderList(List<OrderModel> orderList) {
        this.orderList = orderList;
    }

    @Override
    void onInit(Bundle savedInstanceState) {
        setTitle(R.string.order_list_title);
        orderPresenter = new OrderPresenter(this);
        Date prevMonthDate = DateUtil.minusMonthToDate(new Date(), 1);
        startDate = DateUtil.dateFormatToStr(prevMonthDate);
        endDate = DateUtil.getCurrentDate();
        setDateRangeVal();
        dateSelectDialog = new DoubleDateSelectDialog(this, "1990-01-01", "2099-12-31",
                endDate);
        dateSelectDialog.setOnDateSelectFinished((String s, String s1) -> {
            Log.i(TAG, "s = "+ s+", s1 = "+s1);
            startDate = s;
            endDate = s1;
            setDateRangeVal();
            loadData();
        });
        orderListAdapter = new OrderListAdapter(this, orderList);
        lvOrderList.setAdapter(orderListAdapter);
    }

    private void setDateRangeVal() {
        String dateRange = String.format("%s - %s", startDate, endDate);
        tvDateRange.setText(dateRange);
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.e(TAG, "onResume...");
        loadData();
    }

    @Override
    protected BackListener getCustBackListener() {
        return (view) -> go(UserCenterActivity.class);
    }

    private void loadData() {
        orderPresenter.getSumAmount(startDate, endDate);
        orderPresenter.getList(startDate, endDate);
    }

    @OnClick(R.id.tv_date_range)
    public void chooseDateRange() {
        dateSelectDialog.show();
    }


    @Override
    protected boolean doFailure(CommandType commandType, DataResult dataResult) {
        switch (commandType) {
            case LIST:{ //订单详情数据

                break;
            } case GET:{ // 应收/实收合计数据
                break;
            }
        }
        return false;
    }

    @Override
    protected boolean doSuccess(CommandType commandType, DataResult dataResult) {
        switch (commandType) {
            case LIST:{ //订单列表数据
                setOrderList(dataResult);
                break;
            } case GET:{ //应收/实收合计数据
                setSumAmount(dataResult);
                break;
            }
        }
        return true;
    }

    private void setSumAmount(DataResult dataResult) {
        JSONObject data = (JSONObject)dataResult.getDatas();
        String receivableAmount = data.getString("receivableAmount");
        String receivedAmount = data.getString("receivedAmount");
        tvReceivableAmount.setText(receivableAmount);
        tvReceivedAmount.setText(receivedAmount);
    }

    private void setOrderList(DataResult dataResult) {
        JSONObject dataJson = (JSONObject) dataResult.getDatas();
        JSONArray jsonArray = dataJson.getJSONArray("list");
        if (null == jsonArray || jsonArray.isEmpty()) {
            orderList = new ArrayList<>();
        } else {
            orderList = FastJsonUtil.convertJsonToList(jsonArray.toString(), OrderModel.class);
        }
        orderListAdapter.setOrderList(orderList);
        orderListAdapter.notifyDataSetChanged();
    }

}